package poketrade;

import java.io.IOException;

import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import poketrade.entities.User;
import poketrade.managers.UserManager;

/**
 * Servlet implementation class UpgradeMember
 */
@WebServlet("/UpgradeMember")
public class UpgradeMember extends BaseHttpServlet {
	private static final long serialVersionUID = 1L;
    
	@EJB
	private UserManager mgr;
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		if (session == null)
		{
			showMessage(request, response, "Not connected");
			return;
		}
		
		User currentUser = (User) session.getAttribute("user");
		if (currentUser == null)
		{
			showMessage(request, response, "Not connected");
			return;
		}
		
		if (currentUser.getSolde() >= 200.f)
		{
			session.setAttribute("user", mgr.upgradeMember(currentUser.getId()));
			showMessage(request, response, "Félicitation, vous êtes maintenant premium !");
		}
		else
			showMessage(request, response, "Désolé, vous n'avez pas assez sur votre compte :(");
	}

}
